package com.library.controller;

import com.library.bean.ReaderCard;
import com.library.service.BookService;
import com.library.service.LendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;

@Controller
public class LendController {
    @Autowired
    private LendService lendService;

    @Autowired
    private BookService bookService;

    @RequestMapping("/deletebook.html")
    public String deleteBook(HttpServletRequest request, RedirectAttributes redirectAttributes) {
        try {
            long bookId = parseBookId(request);
            if (bookService.deleteBook(bookId)) {
                redirectAttributes.addFlashAttribute("succ", "图书删除成功！");
            } else {
                redirectAttributes.addFlashAttribute("error", "图书删除失败！");
            }
        } catch (NumberFormatException e) {
            redirectAttributes.addFlashAttribute("error", "无效的图书ID");
        }
        return "redirect:/admin_books.html";
    }

    @RequestMapping("/lendlist.html")
    public ModelAndView lendList(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("admin_lend_list");
        modelAndView.addObject("list", lendService.lendList());
        return modelAndView;
    }

    @RequestMapping("/mylend.html")
    public ModelAndView myLend(HttpServletRequest request) {
        ReaderCard readerCard = (ReaderCard) request.getSession().getAttribute("readercard");
        ModelAndView modelAndView = new ModelAndView("reader_lend_list");
        modelAndView.addObject("list", lendService.myLendList(readerCard.getReaderId()));
        return modelAndView;
    }

    @RequestMapping("/deletelend.html")
    public String deleteLend(HttpServletRequest request, RedirectAttributes redirectAttributes) {
        try {
            long serNum = Long.parseLong(request.getParameter("serNum"));
            if (lendService.deleteLend(serNum) > 0) {
                redirectAttributes.addFlashAttribute("succ", "记录删除成功！");
            } else {
                redirectAttributes.addFlashAttribute("error", "记录删除失败！");
            }
        } catch (NumberFormatException e) {
            redirectAttributes.addFlashAttribute("error", "无效的记录编号");
        }
        return "redirect:/lendlist.html";
    }

    @RequestMapping("/lendbook.html")
    public String bookLend(HttpServletRequest request, RedirectAttributes redirectAttributes) {
        try {
            long bookId = parseBookId(request);
            ReaderCard readerCard = (ReaderCard) request.getSession().getAttribute("readercard");
            long readerId = readerCard.getReaderId();
            
            if (lendService.lendBook(bookId, readerId)) {
                redirectAttributes.addFlashAttribute("succ", "图书借阅成功！");
            } else {
                redirectAttributes.addFlashAttribute("error", "图书借阅失败！");
            }
        } catch (NumberFormatException e) {
            redirectAttributes.addFlashAttribute("error", "无效的图书ID");
        } catch (Exception e) {
            redirectAttributes.addFlashAttribute("error", "借阅过程中发生错误：" + e.getMessage());
        }
        return "redirect:/reader_books.html";
    }

    @RequestMapping("/returnbook.html")
    public String bookReturn(HttpServletRequest request, RedirectAttributes redirectAttributes) {
        try {
            long bookId = parseBookId(request);
            ReaderCard readerCard = (ReaderCard) request.getSession().getAttribute("readercard");
            long readerId = readerCard.getReaderId();
            
            if (lendService.returnBook(bookId, readerId)) {
                redirectAttributes.addFlashAttribute("succ", "图书归还成功！");
            } else {
                redirectAttributes.addFlashAttribute("error", "图书归还失败！");
            }
        } catch (NumberFormatException e) {
            redirectAttributes.addFlashAttribute("error", "无效的图书ID");
        } catch (Exception e) {
            redirectAttributes.addFlashAttribute("error", "归还过程中发生错误：" + e.getMessage());
        }
        return "redirect:/reader_books.html";
    }
    
    private long parseBookId(HttpServletRequest request) throws NumberFormatException {
        String bookIdParam = request.getParameter("bookId");
        if (bookIdParam == null || bookIdParam.trim().isEmpty()) {
            throw new NumberFormatException("bookId参数为空");
        }
        // 去除空格并转换
        return Long.parseLong(bookIdParam.trim());
    }
}